package grade.web;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import grade.dao.GradeDao;
import grade.model.Grade;
import net.sf.json.JSONObject;
import util.MyUtils;
import util.Page;

public class GradeSer extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private GradeDao dao = GradeDao.getInstance();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		String method = request.getParameter("method");
		try {
			if(StringUtils.isEmpty(method)){
				list(request,response);
			}else if("list".equals(method)){
				list(request,response);
			}else if("delete".equals(method)){
				delete(request,response);
			}else if("save".equals(method)){
				save(request,response);
			}else if("query".equals(method)){
				query(request,response);
			}else{
				list(request,response);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	/**
	 * 查询 分页、查询条件
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void list(HttpServletRequest req, HttpServletResponse resp)throws Exception{
		int pageNum = Integer.parseInt(req.getParameter("page"));//页数
		int rows = Integer.parseInt(req.getParameter("rows"));//行数
		String name = req.getParameter("name");
		Page page = new Page();
		page.setPageNum(pageNum);
		page.setPageSize(rows);
		Grade grade = new Grade();
		grade.setName(name);
		int total = dao.getCount(grade);
		ResultSet rs = dao.list(page,grade);
		MyUtils.print(rs, resp,total);
	}
	public void delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String ids = request.getParameter("ids");
		int num = dao.delete(ids);
		JSONObject json = new JSONObject();
		if(num>0){
			json.put("success",true);
			json.put("num", num);
		}else{
			json.put("success",false);
		}
		response.getWriter().print(json.toString());
	}
	public void save(HttpServletRequest request, HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String descc = request.getParameter("descc");
		Grade grade = new Grade(id,name,descc);
		boolean flag = true;
		try{
			dao.save(grade);
		}catch (Exception e){
			flag = false;
			e.printStackTrace();
		}
		result.put("success", flag);
		if(flag){
			result.put("msg", "保存成功！");
		}else{
			result.put("msg", "保存失败！啥情况?");
		}
		response.getWriter().println(result.toString());
	}
	public void query(HttpServletRequest request, HttpServletResponse response)throws Exception{
		this.list(request, response);
	}

}
